package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.ViewSelectedFields;


@Repository
public class ViewSelectedFieldsDaoImpl {
	@Autowired
	SessionFactory sessionFactory;
	
	public void addView(ViewSelectedFields viewSelectedField) {
		try{
		sessionFactory.getCurrentSession().save(viewSelectedField);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public List<ViewSelectedFields> getViewSelectedFields(Integer viewId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from ViewSelectedFields where  view.id = :viewId");
		query.setParameter("viewId", viewId);
		@SuppressWarnings("unchecked")
		List<ViewSelectedFields>  viewSelectedFields= query.list();
		if(viewSelectedFields != null && viewSelectedFields.size() > 0)
		return viewSelectedFields;
		else
			return new ArrayList<ViewSelectedFields>();
		
	}
	public ViewSelectedFields getViewSelectedFieldById(Integer Id){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from ViewSelectedFields where  id = :Id");
		query.setParameter("Id", Id);
		@SuppressWarnings("unchecked")
		List<ViewSelectedFields>  viewSelectedFields= query.list();
		System.out.println("><><><><><><+++++++++++++++++++++++++++++"+viewSelectedFields.size());
		if(viewSelectedFields.size() > 0)
		return viewSelectedFields.get(0);
		else
		return null;
		
	}

	public void saveOrupdate(ViewSelectedFields viewSelectedField){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(viewSelectedField);
	}
	
	public ViewSelectedFields loadViewSelectedField(Integer id) {
		ViewSelectedFields viewSelectedField = (ViewSelectedFields)sessionFactory.getCurrentSession().get(ViewSelectedFields.class, id);
		return viewSelectedField;
		
	}
	
	public void deleteView(ViewSelectedFields viewSelectedField) {
		sessionFactory.getCurrentSession().delete(viewSelectedField);
		
	}

}
